/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.HangSanXuatDAO;
import dao.SanPhamDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.CuaHang;
import pojo.HangSanXuat;
import pojo.SanPham;

/**
 *
 * @author LaiHuynh
 */
public class TimKiemSanPham_Dialog extends javax.swing.JDialog {

    private DefaultComboBoxModel dcbm;
    private DefaultTableModel dtm;
    private int maSanPham;
    private double giaBan;

    public double getGiaBan() {
        return giaBan;
    }

    public void setGiaBan(double giaBan) {
        this.giaBan = giaBan;
    }

    public int getMaSanPham() {
        return maSanPham;
    }

    public void setMaSanPham(int maSanPham) {
        this.maSanPham = maSanPham;
    }

    /**
     * Creates new form TimKiemSanPham_Dialog
     */
    public TimKiemSanPham_Dialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.dcbm = new DefaultComboBoxModel();
        //this.jcbbHangSX.setModel(dcbm);
    }

    private void hienThiDanhSach() {
        //doc du lieu
        this.dtm = new DefaultTableModel();
        List<SanPham> ds = SanPhamDAO.layDanhSachSanPham();
        this.dtm.addColumn("Ma San Pham");
        this.dtm.addColumn("Ten San Pham");
        this.dtm.addColumn("Gia Ban");
        this.dtm.addColumn("So Luong Ton");
        this.dtm.addColumn("Hang SX");
        this.dtm.addColumn("Cua Hang");
        for (int i = 0; i < ds.size(); i++) {
            SanPham sp = ds.get(i);
            Vector vt1 = new Vector();
            vt1.add(sp.getMaSanPham());
            vt1.add(sp.getTenSanPham());
            vt1.add(sp.getGiaBan());
            vt1.add(sp.getSoLuongTon());
            HangSanXuat hsx = sp.getHangSanXuat();
            CuaHang ch = sp.getCuaHang();
            vt1.add(hsx.getTenHang());
            vt1.add(ch.getTenCuaHang());
            this.dtm.addRow(vt1);
        }
        this.jtDanhSach.setModel(dtm);
    }

    public void timKiem() {
        try {
//            this.dtm = new DefaultTableModel();
//            Vector vt = (Vector) this.jcbbHangSX.getSelectedItem();
//            int maHangsx = (Integer) vt.get(0);
//            String tenSanPham = this.jtfTenSanPham.getText();
//            List<SanPham> ds = SanPhamDAO.timKiemSanPham(tenSanPham, maHangsx);
//            this.dtm.addColumn("Ma San Pham");
//            this.dtm.addColumn("Ten San Pham");
//            this.dtm.addColumn("Gia Ban");
//            this.dtm.addColumn("So Luong Ton");
//            this.dtm.addColumn("Hang SX");
//            this.dtm.addColumn("Cua Hang");
//            for (int i = 0; i < ds.size(); i++) {
//                SanPham sp = ds.get(i);
//                Vector vt1 = new Vector();
//                vt1.add(sp.getMaSanPham());
//                vt1.add(sp.getTenSanPham());
//                vt1.add(sp.getGiaBan());
//                vt1.add(sp.getSoLuongTon());
//                HangSanXuat hsx = sp.getHangSanXuat();
//                CuaHang ch = sp.getCuaHang();
//                vt1.add(hsx.getTenHang());
//                vt1.add(ch.getTenCuaHang());
//                this.dtm.addRow(vt1);
//            }
//            this.jtDanhSach.setModel(dtm);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfTenSanPham = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jcbbHangSX = new javax.swing.JComboBox();
        jbtChon = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tìm Sản Phẩm");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin sản phẩm"));

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtDanhSach);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách sản phẩm theo thông tin"));

        jLabel1.setText("Tên sản phẩm:");

        jtfTenSanPham.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfTenSanPhamCaretUpdate(evt);
            }
        });
        jtfTenSanPham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfTenSanPhamActionPerformed(evt);
            }
        });

        jLabel2.setText("Hãng sản xuất:");

        jcbbHangSX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbbHangSXActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel1)
                .addGap(34, 34, 34)
                .addComponent(jtfTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jcbbHangSX, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(132, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jcbbHangSX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtChon.setText("Chọn");
        jbtChon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtChonActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtChon)
                        .addGap(18, 18, 18)
                        .addComponent(jbtThoat)
                        .addGap(54, 54, 54))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtChon)
                    .addComponent(jbtThoat))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfTenSanPhamCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfTenSanPhamCaretUpdate
        // TODO add your handling code here:
        this.timKiem();
    }//GEN-LAST:event_jtfTenSanPhamCaretUpdate

    private void jtfTenSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTenSanPhamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfTenSanPhamActionPerformed

    private void jcbbHangSXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbbHangSXActionPerformed
        // TODO add your handling code here:
        this.timKiem();
    }//GEN-LAST:event_jcbbHangSXActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        try {
            List<HangSanXuat> ds = HangSanXuatDAO.layDanhSachHangSanXuat();
            for (int i = 0; i < ds.size(); i++) {
                HangSanXuat hsx = ds.get(i);
                Vector vt = new Vector();
                vt.add(hsx.getMaHangsx());
                vt.add(hsx.getTenHang());
                dcbm.addElement(vt);
            }
            this.jcbbHangSX.setModel(dcbm);
            this.hienThiDanhSach();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_formWindowOpened

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jbtChonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtChonActionPerformed
        // TODO add your handling code here:
        int dong = this.jtDanhSach.getSelectedRow();
        if (dong >= 0) {
            ChiTietHoaDonBanHang frm = new ChiTietHoaDonBanHang();
            this.maSanPham = (Integer) this.jtDanhSach.getValueAt(dong, 0);
            this.giaBan = (Double) this.jtDanhSach.getValueAt(dong, 2);
            this.setVisible(false);
        }
    }//GEN-LAST:event_jbtChonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TimKiemSanPham_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TimKiemSanPham_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TimKiemSanPham_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TimKiemSanPham_Dialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TimKiemSanPham_Dialog dialog = new TimKiemSanPham_Dialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtChon;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JComboBox jcbbHangSX;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextField jtfTenSanPham;
    // End of variables declaration//GEN-END:variables
}
